package com.magicsoft.share.presenter.share;

import com.magicsoft.share.base.BasePresenter;
import com.magicsoft.share.bean.share.Article;
import com.magicsoft.share.constant.APIService;
import com.magicsoft.share.constant.SPKey;
import com.magicsoft.share.rx_retrofit_network.location.retrofit.RetrofitUtil;
import com.magicsoft.share.rx_retrofit_network.location.rxandroid.LoadingTransformer;
import com.magicsoft.share.rx_retrofit_network.location.rxandroid.NetWorkTransformer;
import com.magicsoft.share.rx_retrofit_network.location.rxandroid.SimpleObserver;
import com.magicsoft.share.utils.SPUtils;
import com.magicsoft.share.view.share.IArticleView;

import io.reactivex.annotations.NonNull;

/**
 * Created by lyf on 2017/11/27/027.
 */

public class ArticlePresenter extends BasePresenter<IArticleView> {
    public void getArticleDetail(long articleId) {
        RetrofitUtil.getApi(APIService.class)
                .getArticleDetail(SPUtils.getInstance().getLong(SPKey.ID), articleId)
                .compose(new NetWorkTransformer<Article>())
             //   .compose(new LoadingTransformer<Article>(mContext))
                .subscribe(new SimpleObserver<Article>(mCompositeDisposable) {
                    @Override
                    public void call(@NonNull Article article) {
                        getView().getArticleDetailSuccess(article);
                    }
                });
    }

    public void articleLove(long articleId) {
        RetrofitUtil.getApi(APIService.class)
                .articleLove(SPUtils.getInstance().getString(SPKey.TOKEN),SPUtils.getInstance().getLong(SPKey.ID), articleId)
                .compose(new NetWorkTransformer())
                .compose(new LoadingTransformer(mContext))
                .subscribe(new SimpleObserver(mCompositeDisposable) {
                    @Override
                    public void call(@NonNull Object o) {

                        getView().articleLoveSuccess();
                    }
                });
    }
    public void deleteArticleLove(long articleId) {
        RetrofitUtil.getApi(APIService.class)
                .deleteArticleLove(SPUtils.getInstance().getString(SPKey.TOKEN),SPUtils.getInstance().getLong(SPKey.ID), articleId)
                .compose(new NetWorkTransformer())
                .compose(new LoadingTransformer(mContext))
                .subscribe(new SimpleObserver(mCompositeDisposable) {
                    @Override
                    public void call(@NonNull Object o) {

                        getView().deleteArticleLoveSuccess();
                    }
                });
    }
    public void articleCollection(long articleId) {
        RetrofitUtil.getApi(APIService.class)
                .articleCollection(SPUtils.getInstance().getString(SPKey.TOKEN),SPUtils.getInstance().getLong(SPKey.ID), articleId)
                .compose(new NetWorkTransformer())
                .compose(new LoadingTransformer(mContext))
                .subscribe(new SimpleObserver(mCompositeDisposable) {
                    @Override
                    public void call(@NonNull Object o) {

                        getView().articleCollectionSuccess();
                    }
                });
    }

    public void deleteCollection(long articleId) {
        RetrofitUtil.getApi(APIService.class)
                .deleteCollection(SPUtils.getInstance().getString(SPKey.TOKEN),SPUtils.getInstance().getLong(SPKey.ID), articleId)
                .compose(new NetWorkTransformer())
                .compose(new LoadingTransformer(mContext))
                .subscribe(new SimpleObserver(mCompositeDisposable) {
                    @Override
                    public void call(@NonNull Object o) {

                        getView().deleteCollectionSuccess();
                    }
                });
    }

    public void addFollow(long otherId) {
        RetrofitUtil.getApi(APIService.class).addFollow(SPUtils.getInstance().getString(SPKey.TOKEN), SPUtils.getInstance().getLong(SPKey.ID)
                , otherId)
                .compose(new NetWorkTransformer())
                .compose(new LoadingTransformer(mContext))
                .subscribe(new SimpleObserver(mCompositeDisposable) {
                    @Override
                    public void call(Object o) {

                        getView().followChangeSuccess();
                    }
                });
    }

    public void deleteFollow(long otherId) {
        RetrofitUtil.getApi(APIService.class).deleteFollow(SPUtils.getInstance().getString(SPKey.TOKEN), SPUtils.getInstance().getLong(SPKey.ID), otherId)
                .compose(new NetWorkTransformer())
                .compose(new LoadingTransformer(mContext))
                .subscribe(new SimpleObserver(mCompositeDisposable) {
                    @Override
                    public void call(Object o) {

                        getView().followChangeSuccess();
                    }
                });
    }

}
